<?php

declare(strict_types=1);

namespace app\controller\api;

use app\model\AdminUser;
use app\util\Sms;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use app\util\WechatService;
use app\model\AdminUserData;

/**
 * 驾校平台端二手车
 */
class JxAdminCar extends Base
{
    /**
     *
     *设置二手车价格
     */
    public function setReleaseCarPrice(): Response
    {
        $postData = $this->request->post();
        $admin_id = $postData['admin_id'];
        $release_car_price = $postData['release_car_price'];
        $res = Db::table('jx_system')->where('mark', 'release_car_price')->update(['content' => $release_car_price]);
        if ($res === false) {
            return $this->buildFailed(402, '修改失败');
        }
        return $this->buildSuccess();
    }


    /**
     *
     *获取二手车价格
     */
    public function getReleaseCarPrice(): Response
    {
        $postData = $this->request->post();
        $res = Db::table('jx_system')->where('mark', 'release_car_price')->find();
        if ($res === false) {
            return $this->buildFailed(402, '修改失败');
        }
        return $this->buildSuccess($res);
    }



    //添加或者修改品牌
    public function addCarBrand()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['car_brand_name'] = $param['car_brand_name'];

 
        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_car_brand')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_car_brand')->where('car_brand_name', $param['car_brand_name'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }

            $res = Db::name('jx_car_brand')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }


    //品牌列表
    public function carBrandList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['car_brand_name']) && $param['car_brand_name'] != '') {
            $where[] = ['car_brand_name', 'like', '%' . $param['car_brand_name'] . '%'];
        }

        $list = Db::table('jx_car_brand')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_car_brand')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //品牌详情
    public function cardBrandDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_car_brand')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除品牌
    public function carBrandDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_car_brand')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }


    //二手车管理列表
    public function carList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['c.status'] = array('in', '1,2');
        if (isset($param['car_brand_name']) && $param['car_brand_name'] != '') {
            $where[] = ['car_brand_name', 'like', '%' . $param['car_brand_name'] . '%'];
        }
        if (isset($param['title']) && $param['title'] != '') {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }

        $field = 'c.id,c.title,b.car_brand_name,c.car_age,c.car_price';
        $list = Db::table('jx_car')->alias('c')->join('jx_car_brand b', 'b.id=c.car_brand_id')->field($field)->page((int)$page, (int)$limit)->where($where)->order('c.id desc')->select();
        $count = Db::table('jx_car')->alias('c')->join('jx_car_brand b', 'b.id=c.car_brand_id')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //二手车详情
    public function AdminCarDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $field  = 'c.*,co.user_name,co.nickname,co.user_img,co.user_phone,car_brand_name';
        $data = Db::table('jx_car')->alias('c')->join('jx_coach co', 'co.id=c.coach_id')
        ->join('jx_car_brand cb', 'cb.id=c.car_brand_id')
        ->field($field)->where('c.id', $param['id'])->find();
        if ($data['car_image']) {
            $data['car_image'] = explode(',', $data['car_image']);
        } else {
            $data['car_image'] = [];
        };
        if ($data['driving_image']) {
            $data['driving_image'] = explode(',', $data['driving_image']);
        } else {
            $data['driving_image'] = [];
        }
        //获取车主新


        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }
    
    //删除二手车
    public function CarDel(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_car')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }
}
